﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC
{
	/// <summary>
	/// Interaction logic for StoredDrugPopup.xaml
	/// </summary>
	public partial class StoredDrugPopup : Window
	{
		public StoredDrugPopup()
		{
			this.InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            SetLists();
            DrugComboBox.ItemsSource = drugs;
            ManufacturerComboBox.ItemsSource = manufacturers;
            IssueFormComboBox.ItemsSource = issueForms;
            BoxComboBox.ItemsSource = boxes;
            storedDrug = new DTCDrugStore();
            MainGrid.DataContext = storedDrug;
			
			// Insert code required on object creation below this point.
		}

        private void SetLists()
        {
            drugs = App.DBI.GetAllDrugs();
            manufacturers = App.DBI.GetAllManufacturers();
            issueForms = App.DBI.GetAllIssueForms();
            boxes = App.DBI.GetAllBoxes();
            if (DrugComboBox.ItemsSource != null)
            {
                BoxView = (ListCollectionView)CollectionViewSource.GetDefaultView(DrugComboBox.ItemsSource);
                BoxView.Filter = new Predicate<object>(DrugFilter);
            }
        }

        private ListCollectionView BoxView;
        private List<DTCDrug> drugs;
        private List<DTCIssueForm> issueForms;
        private List<DTCManufacturer> manufacturers;
        private List<DTCBox> boxes;

        public DTCDrug SelectedDrug
        {
            get
            {
                return (DTCDrug)DrugComboBox.SelectedItem;
            }
            set
            {
                if (DrugComboBox.SelectedItem != value)
                    DrugComboBox.SelectedItem = value;
            }
        }
        public DTCIssueForm SelectedIssueForm
        {
            get
            {
                return (DTCIssueForm)IssueFormComboBox.SelectedItem;
            }
            set
            {
                if (IssueFormComboBox.SelectedItem != value)
                {
                    IssueFormComboBox.SelectedItem = value;
                }
            }
        }
        public DTCManufacturer SelectedManufacturer
        {
            get
            {
                return (DTCManufacturer)ManufacturerComboBox.SelectedItem;
            }
            set
            {
                if (ManufacturerComboBox.SelectedItem != value)
                {
                    ManufacturerComboBox.SelectedItem = value;
                }
            }
        }
        public DTCBox SelectedBox
        {
            get
            {
                return (DTCBox)BoxComboBox.SelectedItem;
            }
            set
            {
                if (BoxComboBox.SelectedItem != value)
                {
                    BoxComboBox.SelectedItem = value;
                }
            }
        }


        private DTCDrugStore storedDrug;

        public DTCDrugStore Drug
        {
            get
            {
                return storedDrug;
            }
            set
            {
                if (storedDrug != value)
                {

                    storedDrug = value;
                    DrugComboBox.SelectedItem = storedDrug.Drug;
                    ManufacturerComboBox.SelectedItem = storedDrug.Manufacturer;
                    IssueFormComboBox.SelectedItem = storedDrug.IssueForm;
                    BoxComboBox.SelectedItem = storedDrug.DTCBox;
                }
            }
        }

        private void DrugComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BoxView.Filter = new Predicate<object>(DrugFilter);
        }

        private void IssueFormComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BoxView.Filter = new Predicate<object>(DrugFilter);
        }

        private void ManufacturerComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BoxView.Filter = new Predicate<object>(DrugFilter);
        }

        private void BoxComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DTCBox selBox = (DTCBox) BoxComboBox.SelectedItem;
            if (DrugComboBox.SelectedItem != selBox.Drug)
                DrugComboBox.SelectedItem = selBox.Drug;
            if (ManufacturerComboBox.SelectedItem != selBox.Manufacturer)
                ManufacturerComboBox.SelectedItem = selBox.Manufacturer;
            if (IssueFormComboBox.SelectedItem != selBox.IssueForm)
                IssueFormComboBox.SelectedItem = selBox.IssueForm;
        }

        private bool DrugFilter(Object item)
        {
            DTCBox box = item as DTCBox;
            if(DrugComboBox.ItemsSource != null)
                if(DrugComboBox.SelectedIndex != -1)
                    if (box.Drug != DrugComboBox.SelectedItem)
                        return false;
            if(ManufacturerComboBox.ItemsSource != null)
                if(ManufacturerComboBox.SelectedIndex != -1)
                    if (box.Manufacturer != ManufacturerComboBox.SelectedItem)
                        return false;
            if(IssueFormComboBox.ItemsSource != null)
                if(IssueFormComboBox.SelectedIndex != -1)
                    if (box.IssueForm != IssueFormComboBox.SelectedItem)
                        return false;
            return true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                //проверяем надо ли добавить в бд ноный DTCDrug
                if (DrugComboBox.SelectedIndex == -1 && DrugComboBox.Text.Length != 0)
                {
                    DTCDrug drug = new DTCDrug();
                    drug.Name = DrugComboBox.Text;
                    App.DBI.Db.DTCDrugs.InsertOnSubmit(drug);
                }
                //Проверяем, надо ли добавить в бд новый DTCManufacturer
                if (ManufacturerComboBox.SelectedIndex == -1 && ManufacturerComboBox.Text.Length != 0)
                {
                    DTCManufacturer man = new DTCManufacturer();
                    man.Name = ManufacturerComboBox.Text;
                    App.DBI.Db.DTCManufacturers.InsertOnSubmit(man);
                }
                //Проверяем, надо ли добавить в бд новый DTCIssue form
                //Проверять, надо ли добавить в бд новый DTCBox не надо, поскольку это будет
                //делатсья при внесении нового DTCBox в диалоговом окне
                int count=0;
                try
                {
                    Int32.TryParse(CountTextBox.Text, out count);
                }
                catch
                {
                    //show error
                    int i = 0;
                }
                if (count == 0)
                {
                    if (storedDrug.StoredBoxID != 0)
                        App.DBI.Db.DTCDrugStores.DeleteOnSubmit(storedDrug);
                }
                else
                {
                    if (BoxComboBox.SelectedItem != null)
                    {

                        if (storedDrug.DTCBox == null)
                        {
                            storedDrug.DTCBox = (DTCBox) BoxComboBox.SelectedItem;
                            App.DBI.Db.DTCDrugStores.InsertOnSubmit(storedDrug);
                        }
                        storedDrug.Count = count;
                        App.DBI.Db.SubmitChanges();
                    }
                }
            }
            if (DialogResult == false)
            {
                if(storedDrug.StoredBoxID != 0)
                    App.DBI.Db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, storedDrug);
                //если был добавлен новый DTCBox, удаляем его
            }


        }

	}
}